package com.eostoken.eos_wallet.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.MapUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseActivity;
import com.eostoken.eos_wallet.bean.NodeIndexBean;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.mvp.mode.AppsModel;
import com.eostoken.eos_wallet.mvp.presenter.AppsPresenterImpl;
import com.eostoken.eos_wallet.mvp.view.AppsView;
import com.eostoken.eos_wallet.utils.RSAUtils;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.qmuiteam.qmui.layout.QMUILayoutHelper;
import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.eostoken.eos_wallet.utils.Constants.NODE_INDEX;

public class MangoNodeActivity extends BaseActivity<AppsModel, AppsView, AppsPresenterImpl> implements AppsView {

    @BindView(R.id.topBar)
    QMUITopBar topBar;
    @BindView(R.id.text1)
    AppCompatTextView text1;
    @BindView(R.id.text2)
    AppCompatTextView text2;
    @BindView(R.id.text3)
    AppCompatTextView text3;
    @BindView(R.id.text4)
    AppCompatTextView text4;
    @BindView(R.id.text5)
    AppCompatTextView text5;
    @BindView(R.id.totalMortgageVolumeTv)
    AppCompatTextView totalMortgageVolumeTv;
    @BindView(R.id.accumulativeDestructionTv)
    AppCompatTextView accumulativeDestructionTv;
    @BindView(R.id.motivateIndexTv)
    AppCompatTextView motivateIndexTv;
    @BindView(R.id.yesterdayIncentiveTv)
    AppCompatTextView yesterdayIncentiveTv;
    @BindView(R.id.yesterdayOrderValueTv)
    AppCompatTextView yesterdayOrderValueTv;
    @BindView(R.id.layout3titleLayout)
    RelativeLayout layout3titleLayout;
    @BindView(R.id.layout3)
    QMUILinearLayout layout3;
    @BindView(R.id.layout4)
    QMUILinearLayout layout4;
    @BindView(R.id.bottomLayout)
    QMUILinearLayout bottomLayout;
    @BindView(R.id.layout1)
    QMUILinearLayout layout1;
    @BindView(R.id.layout2)
    QMUILinearLayout layout2;

    private int mRadius;
    private MgpWallet wallet;
    private String accountName;
    private String privateKey;
    private NodeIndexBean nodeIndexBean;

    @Override
    public int getContentView() {
        return R.layout.fragment_mortgage_big;
    }

    @Override
    protected void initData() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.app_color_blue));
        BarUtils.setStatusBarLightMode(this, false);
        mRadius = QMUIDisplayHelper.dp2px(this, 10);
        wallet = WalletUtils.getCurrentWallet(this);
        privateKey = wallet.getPrivateKey();
        accountName = wallet.getCurrentAccountName();
    }

    public void lazyLoad() {
        try {
            Map params = MapUtils.newHashMap();
            params.put("address", accountName);
            String jsonData2 = GsonUtils.toJson(params);
            String content = RSAUtils.encrypt(jsonData2);
            presenter.requestPortData(content, NODE_INDEX);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initView() {
        topBar.setTitle(getString(R.string.str_mango_node_t));
        topBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        layout1.setRadius(mRadius);
        layout2.setRadius(mRadius);
        text1.setText(getString(R.string.str_yesterday_incentive));
        text2.setText(getString(R.string.str_network_node));
        layout3.setRadius(mRadius, QMUILayoutHelper.HIDE_RADIUS_SIDE_BOTTOM);
        text3.setText(getString(R.string.str_yesterday_performance));
//        text4.setText(getString(R.string.str_yesterday_performance));
        text5.setText(getString(R.string.str_node_level));
        layout4.setVisibility(View.GONE);
        bottomLayout.setVisibility(View.GONE);
        yesterdayOrderValueTv.setVisibility(View.GONE);

        updataView();
    }

    private void updataView() {
        if (nodeIndexBean != null) {
            NodeIndexBean.DataBean dataBean = nodeIndexBean.getData();
            if (dataBean != null) {
                totalMortgageVolumeTv.setText(ObjectUtils.isEmpty(dataBean.getYesterdayMoney()) ? "0.0000" : dataBean.getYesterdayMoney());
                accumulativeDestructionTv.setText(dataBean.getNodeNum() + "");
                text4.setText(ObjectUtils.isEmpty(dataBean.getTeamMoney()) ? "0.0000" : dataBean.getTeamMoney());
                yesterdayIncentiveTv.setText("$" + (ObjectUtils.isEmpty(dataBean.getTeamValue()) ? "0.0000" : dataBean.getTeamValue()));
                motivateIndexTv.setText(ObjectUtils.isEmpty(dataBean.getNodeLevel()) ? "P0" : dataBean.getNodeLevel());
            }
        } else {
            totalMortgageVolumeTv.setText("0.0000");
            accumulativeDestructionTv.setText("0");
            text4.setText("0.0000");
            yesterdayIncentiveTv.setText("$0.00");
            motivateIndexTv.setText("P0");
        }
    }

    @Override
    public void updateView(Object data, int responseSuccessCode) {
        switch (responseSuccessCode) {
            case NODE_INDEX:
                if (data != null) {
                    nodeIndexBean = GsonUtils.fromJson(data.toString(), NodeIndexBean.class);
                    if (nodeIndexBean.getCode() == 0) {
                        NodeIndexBean.DataBean dataBean = nodeIndexBean.getData();
                        if (dataBean != null) {
                            updataView();
                        }
                    } else {
                        ToastUtils.showShort(nodeIndexBean.getMsg());
                    }
                }
                break;
        }
    }

    @Override
    public void onFailure(Object data, int responseFailCode) {

    }

    @Override
    public void updateView(boolean success, Object o1, Object o2) {

    }

    @Override
    public AppsModel createModel() {
        return new AppsModel();
    }

    @Override
    public AppsView createView() {
        return this;
    }

    @Override
    public AppsPresenterImpl createPresenter() {
        return new AppsPresenterImpl(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        lazyLoad();
    }

    @Override
    public boolean isShowState() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
